package com.hellorxjava.demo;


import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.CompletableEmitter;
import io.reactivex.rxjava3.core.CompletableObserver;
import io.reactivex.rxjava3.core.CompletableOnSubscribe;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.core.SingleEmitter;
import io.reactivex.rxjava3.core.SingleObserver;
import io.reactivex.rxjava3.core.SingleOnSubscribe;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * Completable不关心数据只关心结果
 */
public class RXJavaDemo3 {
    public static void main(String[] args) {
        Completable.create(new CompletableOnSubscribe() {
            @Override
            public void subscribe(@NonNull final CompletableEmitter emitter) throws Throwable {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(3000);
                            emitter.onComplete();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }).start();
            }
        }).subscribe(new CompletableObserver() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                System.out.println("onSubscribe1");
            }

            @Override
            public void onComplete() {
                System.out.println("onComplete1");
            }

            @Override
            public void onError(@NonNull Throwable e) {
                System.out.println("onComplete1" + e.getMessage());
            }
        });


        Completable.complete().subscribe(new CompletableObserver() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                System.out.println("onSubscribe2");
            }

            @Override
            public void onComplete() {
                System.out.println("onComplete2");
            }

            @Override
            public void onError(@NonNull Throwable e) {
                System.out.println("onError2" + e.getMessage());
            }
        });

        Completable.error(new Throwable("Complete Error")).subscribe(new CompletableObserver() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                System.out.println("onSubscribe3");
            }

            @Override
            public void onComplete() {
                System.out.println("onComplete3");
            }

            @Override
            public void onError(@NonNull Throwable e) {
                System.out.println("onError3" + e.getMessage());
            }
        });
    }
}
